// judge.js
export default {
    getColumns(matrix) {
        let columns = [];
        for (let col = 0; col < matrix[0].length; col++) {
            let column = [];
            for (let row = 0; row < matrix.length; row++) {
                column.push(matrix[row][col]);
            }
            columns.push(column);
        }
        return columns;
    },

    convertToCountingArray(column) {
        let countArray = [];
        let count = 0;

        for (let i = 0; i < column.length; i++) {
            if (column[i] === 1) {
                count++;
            } else if (count > 0) {
                countArray.push(count); // 当遇到 0 或结束时，记录当前连续的 1 的个数
                count = 0;
            }
        }

        // 如果列的末尾还剩下连续的1，应该把它加进去
        if (count > 0) {
            countArray.push(count);
        }

        return countArray;
    },

    checkMatrix(matrix, rules) {
        let columns = this.getColumns(matrix);

        for (let i = 0; i < columns.length; i++) {
            let column = columns[i];
            let countArray = this.convertToCountingArray(column);

            if (JSON.stringify(countArray) !== JSON.stringify(rules[i])) {
                return false;
            }
        }

        return matrix;
    },

    checkMultipleMatrices(matrices, rules) {
        let validMatrices = [];

        for (let matrix of matrices) {
            let result = this.checkMatrix(matrix, rules);
            if (result) {
                validMatrices.push(result);
            }
        }

        return validMatrices;
    }
}



